﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Data.SqlClient;
using Jy123.Web.AppCode;

namespace Jy123.Web.Manage
{
    public partial class MgConfig : System.Web.UI.Page
    {
        /// <summary>
        /// 管理员
        /// </summary>
        protected Model.Manage_Main manage;
        /// <summary>
        /// 权限
        /// </summary>
        protected Model.Manage_Right mRight;
        /// <summary>
        /// 提示框类型
        /// </summary>
        protected enum MsgType
        {
            /// <summary>
            /// 成功
            /// </summary>
            ok,
            /// <summary>
            /// 错误
            /// </summary>
            error,
            /// <summary>
            /// 警告
            /// </summary>
            warning
        }
        private string ok = @"
        <div style=""padding: 0px 0.7em; margin-top: 20px;"" class=""ok ui-corner-all"">
            <p>
                <span style=""float: left; margin-right: 0.3em"" class=""ui-icon ui-icon-circle-check""></span>
                <strong>成功</strong> {0} </p>
        </div>";
        private string warning = @"
        <div style=""padding: 0px 0.7em; margin-top: 20px;"" class=""ui-state-highlight ui-corner-all"">
            <p>
                <span style=""float: left; margin-right: 0.3em"" class=""ui-icon ui-icon-info""></span>
                <strong>警告</strong> {0} </p>
        </div>";
        private string error = @"
        <div style=""padding: 0px 0.7em;"" class=""ui-state-error ui-corner-all"">
            <p>
                <span style=""float: left; margin-right: 0.3em"" class=""ui-icon ui-icon-alert""></span>
                <strong>错误:</strong> {0} </p>
        </div>";
        private string message;
        /// <summary>
        /// 提示信息
        /// </summary>
        public string Message
        {
            get { return message; }
            set { message = value; }
        }
        private string GetTypeMsg(string content, MsgType mtype)
        {
            string typeMsg = string.Empty;
            switch (mtype)
            {
                case MsgType.ok: typeMsg = string.Format(ok, content); break;
                case MsgType.error: typeMsg = string.Format(error, content); break;
                case MsgType.warning: typeMsg = string.Format(warning, content); break;
                default: typeMsg = content; break;
            }
            return typeMsg;
        }
        private bool SetSesson()
        {
            /*
            Session["SManage"] = new Model.Manage_Main()
            {
                MName = "SuperStar",
                FMRight = 1
            };
            Session["MRight"] = new Model.Manage_Right()
            {
                FMPage = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20",
                RName = "超级管理员"
            };*/
            if (Session["SManage"] == null)
            {
                return false;
            }
            else
            {
                manage = (Model.Manage_Main)Session["SManage"];
                mRight = new BLL.Manage_Right().GetModel(Convert.ToInt32(manage.FMRight));
                Session["MRight"] = mRight;
                return true;
            }
        }
        /// <summary>
        /// 权限
        /// </summary>
        /// <param name="page">当前页面</param>
        protected void Verify(string page)
        {
            if (SetSesson())
            {
                if (page != "#" && VerifyPage(page) == -1)
                {
                    GoToTop("/manage/index.html");
                }
            }
            else
            {
                GoToTop("/manage/Login.aspx");
            }
        }
        private int VerifyPage(string page)
        {
            if (Session["PageItems"] == null)
            {
                string rightstr = string.Empty;
                if (mRight.FMPage != "#")
                {
                    rightstr = " WHERE [ID] IN({0})";
                    rightstr = string.Format(rightstr, mRight.FMPage);
                }
                string strSQL = "SELECT Url FROM Manage_Page";
                SqlDataReader dr = DBUtility.DbHelperSQL.ExecuteReader(string.Format(strSQL, rightstr));
                StringBuilder strUrl = new StringBuilder();
                while (dr.Read())
                {
                    strUrl.Append(dr[0].ToString() + "|");
                }
                dr.Close();
                Session["PageItems"] = strUrl.ToString();
            }
            return Session["PageItems"].ToString().IndexOf(page);
        }
        /// <summary>
        /// 页面置顶跳转
        /// </summary>
        /// <param name="url">跳转页面</param>
        protected void GoToTop(string url)
        {
            Response.Write("<script language = javascript>window.open('" + url + "','_top')</script>");
            Response.End();
        }
        /// <summary>
        /// 弹出层
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="mtype">消息类型</param>
        /// <returns></returns>
        protected string MessageBox(string content, MsgType mtype)
        {
            StringBuilder HTML = new StringBuilder();
            HTML.AppendFormat("<div id=\"dialog\" title=\"提示\" class=\"hid\">{0}</div>", GetTypeMsg(content, mtype));
            HTML.Append(@"
            <script type=""text/javascript"">
                $(function() 
                {
                    $.ui.dialog.defaults.bgiframe = true;
        	        $('#dialog').dialog({
				        buttons: {
					        ""关闭"": function() { 
						        $(this).dialog('close'); 
					        }
				        }
			        });
                });
            </script>");
            return HTML.ToString();
        }
        /// <summary>
        /// 弹出层
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="content">内容</param>
        /// <param name="mtype">消息类型</param>
        /// <returns></returns>
        protected string MessageBox(string title, string content, MsgType mtype)
        {
            StringBuilder HTML = new StringBuilder();
            HTML.AppendFormat("<div id=\"dialog\" title=\"{0}\" class=\"hid\">{1}</div>", title, GetTypeMsg(content, mtype));
            HTML.Append(@"
            <script type=""text/javascript"">
                $(function() 
                {
                    $.ui.dialog.defaults.bgiframe = true;
        	        $('#dialog').dialog({
					        buttons: {
						        ""关闭"": function() { 
							        $(this).dialog('close'); 
						        }
					        }
				        });
                });
            </script>");
            return HTML.ToString();
        }
        /// <summary>
        /// 分页字符串
        /// </summary>
        /// <param name="count">页总数</param>
        /// <param name="now">当前页</param>
        /// <param name="href">超链接字符串，可加$num$替换页码</param>
        /// <returns>分页字符串</returns>
        protected string PageString(int count, int now, string href)
        {
            MoonPage mp = new MoonPage();
            mp.Count = count;
            return mp.GetStrPage(now, "btnsty", "btnsty cred", href);
            //"fillhtml(\"div1\",\"xxxx.ashx?page=$num$\")"
        }
    }
}
